/* eslint-disable*/
const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack')
function resolve(dir) {
  return path.join(__dirname, dir)
}
// CDN外链，会插入到index.html中
const cdn = {
  // 开发环境
  dev: {
    css: [],
    js: []
  },
  // 生产环境
}
// vue.config.js
module.exports = {
  /*
    Vue-cli3:
    Crashed when using Webpack `import()` #2463
    https://github.com/vuejs/vue-cli/issues/2463
   */
  // 如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  // 打包app时放开该配置
  // publicPath:'./',
  configureWebpack: config => {
    config.externals = {
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'vuex': 'Vuex',
      'axios': 'axios',
      'ant-design-vue': 'antd',
      // 'moment':'moment'
      '@antv/g2': 'G2',
      '@antv/data-set':'DataSet',
      // 'moment': 'moment',
      // '@toast-ui/editor':'editor',
      // 'ant-design-vue':'antDesignVue',
      // '@tinymce/tinymce-vue':'tinymceVue',
      // 'tinymce':'tinymce'
    }
    // 生产环境取消 console.log
    if (process.env.NODE_ENV === 'production') {
      console.log("===============================:",process.env.NODE_ENV)
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
      config.plugins.push(
        // 使用UglifyJsPlugin去掉console 可以略微降低文件大小
        new UglifyJsPlugin({
          uglifyOptions: {
            compress: {
              drop_debugger: true,
              drop_console: true, // 生产环境自动删除console
              pure_funcs: ['console.log'], // 移除console
            },
            warnings: false,
          },
          sourceMap: false,
          parallel: true, // 使用多进程并行运行来提高构建速度。默认并发运行数：os.cpus().length - 1。
        }),
      );
    }
    config.resolve.extensions = ['.js', '.json', '.vue','.less'];

  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))
      .set('@api', resolve('src/api'))
      .set('@assets', resolve('src/assets'))
      .set('@comp', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@layout', resolve('src/layout'))
      .set('@static', resolve('src/static'))
      .set('@mobile', resolve('src/modules/mobile'))
      .set('@page-modules', resolve('src/views/page-modules'))
    // config.plugin('compressionPlugin').use(new CompressionPlugin({
    //   test: /\.js$|.\css|.\less/, // 匹配文件名
    //   threshold: 10240, // 对超过10k的数据压缩
    //   minRatio: 0.8,
    //   algorithm: 'gzip',
    //   deleteOriginalAssets: false // 不删除源文件
    // }))
    // 生产环境，开启js\css压缩
    if (process.env.NODE_ENV === 'production') {
      // config.plugin('compressionPlugin').use(new CompressionPlugin({
      //   test: /\.js$|.\css|.\less/, // 匹配文件名
      //   threshold: 10240, // 对超过10k的数据压缩
      //   minRatio: 0.8,
      //   algorithm: 'gzip',
      //   deleteOriginalAssets: false // 不删除源文件
      // }))
      config.optimization.splitChunks({
        chunks:'async',
        cacheGroups: {
          common: {
            name: 'chunk-common', // 打包后的文件名
            minChunks: 2,
            maxInitialRequests: 5,
            maxSize: 200000,
            minSize: 0,
            priority: 1,
            enforce: false
            // reuseExistingChunk: true
          },
          vendors: {
            name: 'chunk-vendors',
            test: /[\\/]node_modules[\\/]/,
            minChunks: 2,
            maxSize: 200000,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 2,
            reuseExistingChunk: true,
            enforce: false
          },
          components: {
            name: 'chunk-components',
            test: /[\\/]src[\\/]components[\\/]/,
            minChunks: 2,
            maxSize: 200000,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 2,
            reuseExistingChunk: true,
            enforce: false
          }
          // antDesignVue: {
          //   name: 'chunk-ant-design-vue',
          //   test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
          //   // chunks: 'initial',
          //   priority: 3,
          //   reuseExistingChunk: true,
          //   enforce: true
          // }
        }
      })
    }else{
      // config.plugin('BundleAnalyzerPlugin').use(new BundleAnalyzerPlugin({ analyzerPort: 8919 }))
    }
    config.plugin('webpack').use(new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn/))

    // 配置 webpack 识别 markdown 为普通的文件
    config.module
      .rule('markdown')
      .test(/\.md$/)
      .use()
      .loader('file-loader')
      .end()

    // config.module
    //   .rule('vue')
    //   .test(/\.vue$/)
    //   .use().loader('vue-loader').options({
    //     cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/vue-loader'),
    //       cacheIdentifier: 'cache-loader:{version} {process.env.NODE_ENV}'
    //   }).end()
    // config.module
    //   .rule('js')
    //   .test(/\.js$/)
    //   .use()
    //   .loader('babel-loader?cacheDirectory=true').options({
    //     exclude:'/node_modules/'
    //   })
    //   .end()
  },

  css: {
    loaderOptions: {
      less: {
        modifyVars: {
          /* less 变量覆盖，用于自定义 ant design 主题 */
          'primary-color': '#1890FF',
          'link-color': '#1890FF',
          'border-radius-base': '4px',
          'font-size-base': '14px'
        },
        javascriptEnabled: true
      }
    }
  },
  devServer: {
    open:true,
    port: 8081,
    overlay: {
      warnings: true,
      errors: true
    },
    proxy: {
      /* '/api': {
         target: 'https://mock.ihx.me/mock/5baf3052f7da7e07e04a5116/antd-pro', //mock API接口系统
         ws: false,
         changeOrigin: true,
         pathRewrite: {
           '/cnForm-boot': ''  //默认所有请求都加了jeecg-boot前缀，需要去掉
         }
       }, */
      '/huishou/':{
        // target: 'http://125.64.43.53:10012', //远程
        target: 'http://localhost:10013',
        ws: false,
        changeOrigin: true,
        pathRewrite: {
          '/huishou/': '/huishou/'
        }
      },
      '/mdm/':{
        // target: 'http://125.64.43.53:10012', //远程
        target: 'http://localhost:10012',
        ws: false,
        changeOrigin: true,
        pathRewrite: {
          '/mdm/': '/mdm/'
        }
      },
      '/tpm/':{
        // target: 'http://125.64.43.53:10012', //远程
        target: 'http://localhost:8308',
        ws: false,
        changeOrigin: true,
        pathRewrite: {
          '/tpm/': '/tpm/'
        }
      },
      '/order/': {
        // target: 'http://125.64.43.53:10011', //远程
        target: 'http://localhost:10011', //本地
        // target: 'http://www.chaonatop.cn', // 本地nginx
        ws: false,
        changeOrigin: true,
        pathRewrite: {
          '/order/': '/order/'
        }
      },
      '/upload': {
        // target: 'http://125.64.43.53:10011', //远程
        target: 'http://www.chaonatop.cn', // 本地nginx
        // target: 'http://localhost:8306', //本地
        ws: false,
        changeOrigin: true
        // pathRewrite: {
        //   '/data': ''  //
        // }
      },
      // '/dms/': {
      //   // target: 'http://125.64.43.53:10011', //远程
      //   target: 'http://192.168.31.73:8082', // 本地nginx
      //   // target: 'http://localhost:8306', //本地
      //   ws: false,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dms': ''  //
      //   }
      // }
    }
  },
  parallel: require('os').cpus().length > 1,
  lintOnSave: false
}
